package com.yuanfeng.action.admin.book.web;

import com.opensymphony.xwork2.ActionSupport;
import com.yuanfeng.pojo.Books;
import com.yuanfeng.pojo.Types;
import com.yuanfeng.action.admin.book.service.BookService;
import com.yuanfeng.action.admin.book.service.BookServiceImpl;
import com.yuanfeng.action.admin.type.service.TypeService;
import com.yuanfeng.action.admin.type.service.TypeServiceImpl;
import com.yuanfeng.util.WebUtil;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

/**
 * 图书action
 */

public class BookAction extends ActionSupport {

    /**
     * TypeService的依赖注入
     */
    private TypeService typeServiceImpl;
    /**
     * BookService的依赖注入
     */
    private BookService bookServiceImpl;
    /**
     * 得到上传的文件
     */
    private File img;
    /**
     * 得到文件的类型
     */
    private String imgContentType;
    /**
     * 得到文件的名称
     */
    private String imgFileName;
    /**
     * 实例化一个Book对象
     */
    private Books book;

    /**
     * Book的set方法
     * @param book
     */
    public void setBook(Books book) {
        this.book = book;
    }
    /**
     * Book的get方法
     * @return
     */
    public Books getBook() {
        return book;
    }


    public void setBookServiceImpl(BookServiceImpl bookServiceImpl) {
        this.bookServiceImpl = bookServiceImpl;
    }

    public void setTypeServiceImpl(TypeServiceImpl typeServiceImpl) {
        this.typeServiceImpl = typeServiceImpl;
    }

    /**
     * 文件上传的Img
     * @param img
     */
    public void setImg(File img) {
        this.img = img;
    }

    /**
     * 得到文件上传的img用来封装
     * @return
     */
    public File getImg() {
        return img;
    }

    /**
     * 得到上传的文件的类型
     * @return
     */
    public String getImgContentType() {
        return imgContentType;
    }

    /**
     * 文件上传的set方法
     * @param imgContentType
     */
    public void setImgContentType(String imgContentType) {
        this.imgContentType = imgContentType;
    }

    /**
     * 得到img的文件名称
     * @return
     */
    public String getImgFileName() {
        return imgFileName;
    }

    /**
     * 设置Img的文件名称
     * @param imgFileName
     */
    public void setImgFileName(String imgFileName) {
        this.imgFileName = imgFileName;
    }

    /**
     * 查看分类
     * @return
     */
    public String toAdd() {
        HttpServletRequest request = ServletActionContext.getRequest();
        //查所有分类
        List<Types> types = typeServiceImpl.queryTypes();
        //放到request
        request.setAttribute("types", types);
        //转发到添加图书的页面
        return "toAdd";
    }
    /**
     * 保存图书
     * 其中需要的部分操作在代码中注释可以明了
     * @return
     * @throws Exception
     */
    public String save() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        //得到实际的路径
        String path = ServletActionContext.getServletContext().getRealPath("/imgs");
        File uploadPath = new File("e:/temps");
        if(!uploadPath.exists()) {
             uploadPath.mkdir();
        }
        //把文件拷贝到一个目录中,一般有错误断点才会直接进入一个方法中去
        FileUtils.copyFileToDirectory(img, uploadPath);
        String fileName = null;
        List<String> types = new ArrayList<String>();
        fileName = imgFileName;
        types.add(book.getTypes()[0]);
        //设置图书的id
        book.setId(UUID.randomUUID().toString().replace("-", ""));
        //设置上传的时间
        book.setOnlinetime(WebUtil.getDateStr());
        //设置图片路径
        book.setImg(fileName);
        //把上传的封面图片拷贝到img目录下
        FileUtils.copyInputStreamToFile(new FileInputStream(img), new File(path+"/"+fileName));
        //得到类型的个数
        int size = types.size();
        String[] typesArr = new String[size];
        for (int i = 0; i < size; i++) {
            typesArr[i] = types.get(i);
        }
        book.setTypes(typesArr);
        //组成book信息
        bookServiceImpl.save(book);
        request.setAttribute("success", "success");
        return "save";
    }

}

